package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/16 15:00
 */
public class LC60排列序列 {

    public String getPermutation(int n, int k) {
        StringBuilder ans = new StringBuilder();
        boolean[] st = new boolean[10];
        for (int i = 0; i < n; i++) {
            int fact = 1;
            for (int j = 1; j <= n - i - 1; j++) fact *= j;
            for (int j = 1; j <= n; j++) {
                if (!st[j]) {
                    if (fact < k) k -= fact;
                    else {
                        ans.append(j);
                        st[j] = true;
                        break;
                    }
                }
            }
        }
        return ans.toString();
    }
}
